package com.lcz.mytest.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lcz.mytest.R;
import com.lcz.mytest.adapter.CollectAdapter;
import com.lcz.mytest.db.UserService;
import com.lcz.mytest.domain.User;
import com.lcz.mytest.widget.ToolBarActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/23.
 */
public class CollectActivity extends ToolBarActivity {

    private PullToRefreshListView collect_list;
    private List<User> collectList = new ArrayList<>();
    private TextView collect_tip;
    private CollectAdapter adapter;

    @Override
    protected void initView() {
        super.initView();
        this.setTitle("我的收藏");
        //获取收藏列表
        UserService service = new UserService(this);
        final List<User> codeList = service.collectList(MainActivity.USERNAME);
        if (codeList == null) {
            collect_tip.setVisibility(View.VISIBLE);
            collect_list.setVisibility(View.GONE);
        } else {
            collect_tip.setVisibility(View.GONE);
            collect_list.setVisibility(View.VISIBLE);
            upData(codeList);
        }
        //上下拉刷新
        collect_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (codeList == null) {
                    collect_tip.setVisibility(View.VISIBLE);
                } else {
                    collect_tip.setVisibility(View.GONE);
                    upData(codeList);
                    collect_list.onRefreshComplete();//刷新结束
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        collect_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String type = codeList.get(position - 1).getDataType();
                if (type.equals("历史数据")) {
                    Intent intent = new Intent(CollectActivity.this, PastStockChartActivity.class);
                    intent.putExtra("symbol", codeList.get(position - 1).getStockCode());
                    CollectActivity.this.startActivity(intent);
                } else if (type.equals("实时数据")) {
                    Intent intent = new Intent(CollectActivity.this, PresentStockChartActivity.class);
                    intent.putExtra("symbol", codeList.get(position - 1).getStockCode());
//                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    CollectActivity.this.startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void findViewById() {
        super.findViewById();
        setContentView(R.layout.activity_collect);
        collect_list = (PullToRefreshListView) findViewById(R.id.collect_list);
        collect_tip = (TextView) findViewById(R.id.collect_tip);
        collect_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);//下拉刷新
    }

    //页面更新处理
    private void upData(List<User> list) {
        collectList.clear();//下拉刷新时清空
        collectList.addAll(list);
        if (adapter == null) {
            adapter = new CollectAdapter(collectList, this);
            collect_list.setAdapter(adapter);
        } else {
            adapter.upDataList(collectList);
            adapter.notifyDataSetChanged();
        }
    }
}
